
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    double (*area)(void *);
}Shape;

typedef struct {
    Shape shape;
    double radius;
}Circle;

typedef struct {
    Shape shape;
    double width;
    double height;
}Rectangle;

double circle_area(void *obj){
    Circle* circle =(Circle *)obj;
    return 3.14 *circle->radius * circle->radius;
}

double rectangle_area(void* obj) {

    Rectangle* rect = (Rectangle*)obj;  // 将 void* 转换为 Rectangle* 类型
return rect->width * rect->height;
}


// 初始化圆形init 

void init_circle(Circle* circle, double radius) {

    circle->radius = radius;

    circle->shape.area = circle_area;  // 设置对应的 area 函数

}


// 初始化矩形

void init_rectangle(Rectangle* rect, double width, double height) {

    rect->width = width;

    rect->height = height;

    rect->shape.area = rectangle_area;  // 设置对应的 area 函数

}


// 打印图形面积

void print_area(Shape* shape) {
  
printf("Area: %.2f\n", shape->area(shape));  // 调用对应的 area 函数

}


int main() {
 
// 创建并初始化圆形和矩形

    Circle circle;

    Rectangle rectangle;

  
init_circle(&circle, 5.0);
   
init_rectangle(&rectangle, 4.0, 6.0);

  
// 打印圆形和矩形的面积
  
printf("Circle: ");
  
print_area((Shape*)&circle);  // 将 Circle 转换为 Shape*

  
printf("Rectangle: ");
  
print_area((Shape*)&rectangle);  // 将 Rectangle 转换为 Shape*

   
return 0;

}
